package ucuserslogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteUserPayAccountLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDeleteUserPayAccountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserPayAccountLogic {
	return &DeleteUserPayAccountLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 删除用户保存的支付账户
func (l *DeleteUserPayAccountLogic) DeleteUserPayAccount(in *pb.DeleteUserPayAccountReq) (*pb.DeleteUserPayAccountResp, error) {
	defer utils.RecoverPanic()

	upa, err := l.svcCtx.UserPayAccountAddressModel.FindOne(l.ctx, in.Id)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	if upa.Uid != in.Uid {
		l.Logger.Errorf("no privilege upa uid %d input uid %d ", upa.Uid, in.Uid)
		return nil, xerr.NewGrpcErrCodeMsg(xerr.AuthFail, "no privilege")
	}
	err = l.svcCtx.UserPayAccountAddressModel.Delete(l.ctx, in.Id)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	return &pb.DeleteUserPayAccountResp{}, nil
}
